const gulp = require('gulp')
const babel = require('gulp-babel')
const watch = require('gulp-watch')

let entry = './src/**/*.js'
let entry2 = './src/**/*.json'

function buildDev(done) {
  setTimeout(() => done(), 1000)
  return watch(entry, { ignoreInitial: false })
    .pipe(
      babel({
        babelrc: false,
        plugins: ['@babel/plugin-transform-modules-commonjs']
      })
    )
    .pipe(gulp.dest('dist'))
}
function buildJson() {
  return watch(entry2, { ignoreInitial: false }).pipe(gulp.dest('dist'))
}

function buildProd() {
  return gulp
    .src(entry)
    .pipe(
      babel({
        babelrc: false,
        plugins: ['@babel/plugin-transform-modules-commonjs']
      })
    )
    .pipe(gulp.dest('dist'))
}

let build = null
if (process.env.NODE_ENV === 'development') {
  build = gulp.series(buildDev, buildJson)
}
if (process.env.NODE_ENV === 'production') {
  build = gulp.series(buildProd, buildJson)
}
if (process.env.NODE_ENV === 'test') {
  build = gulp.series(buildProd, buildJson)
}

gulp.task('default', build)
